package com.app.oral.model

import java.time.LocalDateTime

data class User(
    val id: Long? = null,
    val openid: String? = null,
    val nickname: String = "",
    val avatarUrl: String = "",
    val realName: String = "",
    val gender: Gender = Gender.MALE,
    val userType: UserType? = null,
    val classIds: String = "", // 支持多个班级ID，用#分隔，如"1#2#3"
    val accountStatus: AccountStatus = AccountStatus.INCOMPLETE,
    val isActivated: Boolean = false,
    val createdTime: LocalDateTime? = null,
    val updatedTime: LocalDateTime? = null,
    val activatedTime: LocalDateTime? = null,
    val lastLoginTime: LocalDateTime? = null
) {
    /**
     * 获取班级ID列表
     */
    fun getClassIdList(): List<Long> {
        return if (classIds.isBlank()) {
            emptyList()
        } else {
            classIds.split("#").mapNotNull { it.toLongOrNull() }
        }
    }
    
    /**
     * 检查是否管理指定班级
     */
    fun managesClass(classId: Long): Boolean {
        return getClassIdList().contains(classId)
    }
    
    /**
     * 添加班级ID
     */
    fun addClassId(classId: Long): User {
        val currentList = getClassIdList().toMutableList()
        if (!currentList.contains(classId)) {
            currentList.add(classId)
            val newClassIds = currentList.joinToString("#")
            return this.copy(classIds = newClassIds)
        }
        return this
    }
    
    /**
     * 移除班级ID
     */
    fun removeClassId(classId: Long): User {
        val currentList = getClassIdList().toMutableList()
        currentList.remove(classId)
        val newClassIds = currentList.joinToString("#")
        return this.copy(classIds = newClassIds)
    }
    
    /**
     * 设置班级ID列表
     */
    fun setClassIdList(classIdList: List<Long>): User {
        val newClassIds = classIdList.joinToString("#")
        return this.copy(classIds = newClassIds)
    }
    
    // 向后兼容：保留单个classId的getter（返回第一个班级ID）
    val classId: Long?
        get() = getClassIdList().firstOrNull()
}

enum class Gender {
    MALE, FEMALE
}

enum class UserType {
    STUDENT, TEACHER, ADMIN
}

enum class AccountStatus {
    INCOMPLETE, PENDING, APPROVED, REJECTED
}